package com.mymarkdown.app.model;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.collection.ListUtil;
import com.mymarkdown.app.common.constant.Constant;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

import java.util.*;

/**
 * @author zenglun
 * @date 2021/12/6 12:16 上午
 * @description: 用户历史操作的数据bean对象
 */
@Getter
@Setter
@Accessors(chain = true)
public class UserLatelyInfo {
    private String latelyOperationFile;
    private List<String> historyOperationFile = new LinkedList<>();

    public static UserLatelyInfo empty(){
        return new UserLatelyInfo();
    }

    public void addHistoryOperation(String str){
        if (!historyOperationFile.contains(str)){
            this.historyOperationFile.add(str);
        }else{
            //如果存在
            this.historyOperationFile.remove(str);
            this.historyOperationFile.add(str);
        }
    }

    public List<String> getHistoryOperationFile() {
        List<String> result = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(historyOperationFile)){
            result.addAll(ListUtil.split(historyOperationFile, Constant.DEFAULT_HISTORY_NUMBER).get(0));
        }

        return ListUtil.reverse(result);
    }

    public void removeHistoryOperationFile(String path) {
        this.historyOperationFile.remove(path);
    }
}
